import numpy as np
from numpy import pi


def createArray():
    a1 = np.array([1, 23, 45, 68, 3])
    print(a1.shape)
    print(a1 + 100)
    print("---------------------------------------")
    a2 = np.array([[222, 341, 23], [1231, 3444, 1], [123, 33, 4]])
    print("---------------------------------------")
    print(a2.shape)
    print(a2)
    print("取出所有列第二行数据")
    # 重前向后数
    print(a2[:, 2])
    print("取出所有行前两列数据")
    # 0 表示重后向前数
    print(a2[:, 0: 2])
    #
    print("取出第一行到第三行的第一列到第二列的数据")
    # 取出第一行到第三行的第一列到第二列的数据
    print(a2[1:3, 0: 2])
    # a3 = np.zeros()

    print("---------------------------------------")
    print("判断是否等于某个值 222")
    check = a2 == 222
    print(check)
    print(a2[check])
    # 返回结果为True的数据
    print("---------------------------------------")
    # & | 计算
    e = (a2 == 222) & (a2 == 1)
    print(a2[e])
    e1 = (a2 == 222) | (a2 == 1)
    print(a2[e1])

    print("---------------------------------------")
    # 类型转换
    a3 = np.array(["1", "2", "3"])
    print(a3.dtype)
    a3 = a3.astype(int)
    print(a3.dtype)

    print("---------------------------------------")
    print("获取最小值")
    print(a2.min())
    print("获取最大值")
    print(a2.max())
    print("获取最小值的索引")
    print(a2.argmin())
    print("获取最大值的索引")
    print(a2.argmax())
    print("行求和")
    print(a2.sum(axis=1))
    print("列求和")
    print(a2.sum(axis=0))

    print("---------------------------------------")
    # 创建有序的数组
    a3 = np.arange(10)
    print(a3)
    # 转矩阵 10 到 30 步长1
    a4 = np.arange(10, 30, 1).reshape(4, 5)
    print(a4)
    # 查看维度
    print(a4.ndim)
    # 查看大小
    print(a4.size)
    # 创建矩阵 3行4列 默认float
    a5 = np.zeros((3, 4))
    print(a5)
    # 创建矩阵 3行4列 3维度 dtype:int32
    a6 = np.ones((3, 4, 3), dtype=np.int32)
    print(a6)
    # 随机创建
    a6 = np.random.random((3, 4))
    print(a6)
    # 随机创建0到pi总共10个随机数
    a7 = np.linspace(0, 2 * pi, 30, dtype=np.float32)
    print(a7)

    print("---------------------------------------")
    # 加减乘除
    a8 = np.arange(10)
    a9 = np.arange(12, 22)
    print("加")
    print(a8 + a9)
    print("减")
    print(a9 - a8)
    print(a9 - 2)
    print("乘")
    print(a8 * a9)
    print(a8 ** 2)
    print("除")
    print(a8 / a9)
    print("矩阵乘法")
    a10 = np.arange(10).reshape(2, 5)
    a11 = np.arange(12, 22).reshape(2, 5)
    print(a10)
    print(a11)
    # print(a10.dot(a11))
    # print(np.dot(a10, a11))
    print("---------------------------------------")
    # 矩阵变换
    s1 = np.arange(10)
    print(s1)
    s1.shape = (2, 5)
    print(s1)
    print(s1.ravel())
    print(s1.reshape(5, 2))
    print(s1.T)
    print("---------------------------------------")
    # 矩阵拼接
    s2 = np.random.random((3, 4))
    print(s2)
    s3 = np.random.random((3, 4))
    print(s3)
    # 行拼接
    print(np.vstack((s2, s3)))
    # 按列拼接
    print(np.hstack((s2, s3)))

    # 切分
    print("切分")
    s4 = np.random.random((2, 12))
    print(s4)
    # 按行切分
    print(np.hsplit(s4, 3))
    # 按列切分
    print(np.vsplit(s4, 2))
    # 指定位置切分
    print(np.hsplit(s4, (3, 6)))

    # 创建黑色背景
    image = np.zeros((600, 480, 3), np.uint8)
    print("---------------------------------------")
    image2 = np.zeros((600, 480, 3), np.uint8)
    # print(image2)

    # cv2.imshow('back', image2[30:100, 30:100] + 195)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()


def createArray2():
    a = np.random.random((3, 5))
    print(a)
    # 获取每行最大值 axis=0 行  axis=1 列
    index = a.argmax(axis=0)
    print(index)
    # 打印出最大的数
    print(a[index, range(a.shape[1])])

    print("-----------------------------------")

    a2 = np.arange(0, 40, 10)
    print(a2)
    a3 = np.tile(a2, (3, 4))
    print(a3)

    print("--------------------")
    # 排序
    a4 = np.random.random((3, 4))
    print(a4)
    print(np.sort(a4, axis=1))
    # 按索引排序
    print(np.argsort(a4))


def reeadText():
    text = np.genfromtxt('image/test.txt', delimiter=',', dtype=str, skip_header=1)
    print(text)


def readImage():
    pass


def copy():
    a = np.arange(10)
    b = a
    a[0] = 100
    print(a is b)
    print(a)
    print(b)

    # 浅拷贝 虽然不是一个对象 但是数据还是同一份
    b = a.view()
    print(a is b)
    a[0] = 101
    print(a)
    print(b)

    # 深拷贝
    b = a.copy()
    print(a is b)
    a[0] = 102
    print(a)
    print(b)


def check():
    # 矩阵判断
    a1 = np.array([2, 3, 4])
    a2 = [2, 3, 4]
    for i in a1:
        print(a1)
        print(np.allclose(i, a2))


if __name__ == '__main__':
    # createArray()
    # reeadText()
    # createArray2()
    # copy()
    check()
